package net.bmoller.VoteRewards;
 
import java.util.logging.Logger;
 
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;
 
public class Main extends JavaPlugin
{ 
    public static Main plugin;
    public final Logger logger = Logger.getLogger("Minecraft");
   
    public void onDisable()
    {
        PluginDescriptionFile pdffile = this.getDescription();
        this.logger.info(pdffile.getName() + "v" + pdffile.getVersion() + " is now disabled.");
    }   
    public void onEnable()
    {
        PluginDescriptionFile pdffile = this.getDescription();
        this.logger.info(pdffile.getName() + "v" + pdffile.getVersion() + ", is now enabled.");               
    }   
    public boolean onConsoleCommand(ConsoleCommandSender sender, Command cmd, String commandLabel, String[] args) 
    {
        if (cmd.getName().equalsIgnoreCase("reward") && args.length == 1 && !(sender instanceof Player)) 
        {
        	readConsoleCommand(args[0]);
        }
        return false;
    }
   
    public void readConsoleCommand(String targetName)
    {
        int randomNumber = (int)(Math.random() * 10000);
   
	    try
	    {
	        Player targetPlayer = Bukkit.getServer().getPlayerExact(targetName);
	        if (randomNumber >= 0 && randomNumber <= 4999)
	        {
	            String rewardName = "Diamonds!";
	            targetPlayer.sendMessage(ChatColor.RED + "Thanks for voting! You've recieved " + rewardName );
	            getServer().dispatchCommand(getServer().getConsoleSender(), "give " + targetName + " 57 64");
	        }
	        else if (randomNumber >= 5000 && randomNumber <= 9999)
	        {
	            String rewardName = "Stairs!";
	            targetPlayer.sendMessage(ChatColor.BLUE + "Thanks for voting! You've recieved " + rewardName );
	            getServer().dispatchCommand(getServer().getConsoleSender(), "give " + targetName + " 53 64");
	        }
	    }
	    catch (Exception e)
	    {
	        logger.info("Are you sure that the name specified is spelled correctly?");
	    }      
    }      
}